import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from './utils/auth'
import { Message } from 'element-ui'

NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/login'] // no redirect whitelist

router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  if(getToken()) {
    store.dispatch('GetUserInfo').then(() => {
        next();
        NProgress.done()
    }).catch((error) => {
        store.dispatch("LogOut").then(() => {
            Message.error('授权失效，请重新登录')
            router.push('/login')
            NProgress.done()
        })
    })
  } else {
    if(whiteList.indexOf(to.path) !== -1) {
        next()
    }else {
        Message.error('请您先登录')
        next('/login')
        NProgress.done()
    }
  }

})
router.afterEach(() => {
  NProgress.done() // finish progress bar
})